﻿using System.Collections.Generic;
using System.Linq;
using QASPRibbon.Controls;

namespace QASPRibbon.Extensions
{
    internal static class LinqExtensions
    {
        internal static IEnumerable<RibbonControl> AllControls(this IRibbonContainer<RibbonControl> container)
        {
            var res = new List<RibbonControl>();
            foreach (var control in container.Controls.Where(c=> c.Visible))
            {
                if (control is IRibbonContainer<RibbonControl>)
                {
                    if (control is IRibbonMenu)
                    {
                        if (control is RibbonComboBox)
                        {
                            res.AddRange(((RibbonComboBox)control).AllControls());
                        }
                        else if (control is RibbonDropDown)
                        {
                            res.AddRange(((RibbonDropDown)control).AllControls());
                        }
                        else if (control is RibbonFlyoutAnchor)
                        {
                            res.AddRange(((RibbonFlyoutAnchor)control).AllControls());
                        }
                        else if (control is RibbonMRUSplitButton)
                        {
                            res.AddRange(((RibbonMRUSplitButton)control).AllControls());
                        }
                        else if (control is RibbonSplitButton)
                        {
                            res.AddRange(((RibbonSplitButton)control).AllControls());
                        }
                    }
                    else
                    {
                        if (control is RibbonGallery)
                        {
                            res.AddRange(((RibbonGallery)control).AllControls());
                        }
                        else if (control is RibbonMenuSection)
                        {
                            res.AddRange(((RibbonMenuSection)control).AllControls);
                        }
                    }
                    
                }
                res.Add(control);
            }
            return res;
        }

        internal static IEnumerable<RibbonControl> AllControls(this IRibbonMenu menu)
        {
            var res = new List<RibbonControl>();
            foreach (var section in menu.Sections.Where(s => s.Visible))
            {
                res.AddRange(section.AllControls());
                res.Add(section);
            }

            return res;
        }
    }
}
